package cn.acewill.yxd.yunxundian;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.acewill.yxd.yunxundian.model.RMain;
import cn.acewill.yxd.yunxundian.model.rJson;
import cn.acewill.yxd.yunxundian.pubClass.pubClass;

//云巡店数据交互及数据库存储样例
public class  yunxundian extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    DbManager.DaoConfig daoConfig = new DbManager.DaoConfig()
            .setDbName("yxd.db")
            // 不设置dbDir时, 默认存储在app的私有目录.
            //.setDbDir(new File("/sdcard")) // "sdcard"的写法并非最佳实践, 这里为了简单, 先这样写了.
            .setDbVersion(2)
            .setDbOpenListener(new DbManager.DbOpenListener() {
                @Override
                public void onDbOpened(DbManager db) {
                    // 开启WAL, 对写入加速提升巨大
                    db.getDatabase().enableWriteAheadLogging();
                }
            })
            .setDbUpgradeListener(new DbManager.DbUpgradeListener() {
                @Override
                public void onUpgrade(DbManager db, int oldVersion, int newVersion) {
                    // TODO: ...
                    // db.addColumn(...);
                    // db.dropTable(...);
                    // ...
                    // or
                    // db.dropDb();
                }
            });

    protected void getreportStr()
    {
        String url="http://192.168.2.113/yxd/shop/shopservice.aspx";
        Map<String,Object> map=new HashMap<>();
        map.put("task", "aTest");
        pubClass.Post(url, map, new pubClass.MyCallBack<String>(){

            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Gson gson = new Gson();
                Type typeList = new TypeToken<ArrayList<RMain>>() {
                }.getType();
                ArrayList<RMain> videos = gson.fromJson(result, typeList);
                rJson rj=new rJson();
                rj.setjSon(result);

                DbManager db = x.getDb(daoConfig);
                try {
                    db.save(rj);
                } catch (DbException e) {
                    e.printStackTrace();
                }
                //Log.e("result", result.toString());
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);

            }
        });
    }

    protected void getDBstr()
    {
        DbManager db = x.getDb(daoConfig);
        try {
            List<rJson> rj = db.selector(rJson.class).where("id",">",1).findAll();
            Toast.makeText(this, rj.get(0).getjSon(), Toast.LENGTH_SHORT).show();
            int u=rj.get(1).getId();
        }catch (DbException e) {
            e.printStackTrace();
        }
    }
}
